********Tables H.1 and H.2 - Mexico********

*Load data and create differences
cd "$reploc/data/created_data"
use mex_full, clear

sort pid_link mxfls
drop if mxfls==.
keep if panel==1


*Normalize monetary variables
gen assets_ia_use=assets_ia/1000
gen income_ia_use=income_ia/1000
gen savingnettotal_ia_use=savingnettotal_ia/1000
gen liquidassets_ia_use=liquidassets_ia/1000
gen nonliquidassets_ia_use=nonliquidassets_ia/1000
gen homelandassets_ia_use=homelandassets_ia/1000
gen nonhomelandassets_ia_use=nonhomelandassets_ia/1000
gen laborincome_ia_use=laborincome_ia/1000
gen nonlaborincome_ia_use=nonlaborincome_ia/1000
rename nonhomelandassets_ia_use nonhlassets_ia_use

*Generate higher order + quintiles
foreach var of varlist assets_ia_use income_ia_use savingnettotal_ia_use nonliquidassets_ia_use liquidassets_ia_use homelandassets_ia_use nonhlassets_ia_use laborincome_ia_use nonlaborincome_ia_use {

xtile `var'decmxfls2=`var' if mxfls==2, nq(5)
xtile `var'decmxfls3=`var' if mxfls==3, nq(5)

gen `var'dec=`var'decmxfls2 if mxfls==2
replace `var'dec=`var'decmxfls3 if mxfls==3


tab `var'dec, gen(`var'decdum)

forval i=2/4 {

gen `var'term`i'=(`var')^`i'

}
}

keep rbuc* mxfls pid_link inflation imp_age panel birthyr grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? married hhsize hhsize_sq Eddum* feelsafehome fear_assaultday fear_assaultnight hhviol times_assaulted disaster assets_ia_use income_ia_use savingnettotal_ia_use homiciderate birthstate yearprov *decdum* assets_ia_useterm2 assets_ia_useterm3 assets_ia_useterm4 income_ia_useterm2 income_ia_useterm3 income_ia_useterm4 nonliquidassets_ia_use nonliquidassets_ia_useterm2 nonliquidassets_ia_useterm3 nonliquidassets_ia_useterm4 liquidassets_ia_use liquidassets_ia_useterm2 liquidassets_ia_useterm3 liquidassets_ia_useterm4 homelandassets_ia_use nonhlassets_ia_use homelandassets_ia_useterm2 nonhlassets_ia_useterm2 homelandassets_ia_useterm3 nonhlassets_ia_useterm3 homelandassets_ia_useterm4 nonhlassets_ia_useterm4 laborincome_ia_use nonlaborincome_ia_use laborincome_ia_useterm2 nonlaborincome_ia_useterm2 laborincome_ia_useterm3 nonlaborincome_ia_useterm3 laborincome_ia_useterm4 nonlaborincome_ia_useterm4

reshape wide  rbuc*  inflation imp_age panel grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? married hhsize hhsize_sq Eddum* feelsafehome fear_assaultday fear_assaultnight hhviol times_assaulted disaster assets_ia_use income_ia_use savingnettotal_ia_use homiciderate *decdum* assets_ia_useterm2 assets_ia_useterm3 assets_ia_useterm4 income_ia_useterm2 income_ia_useterm3 income_ia_useterm4 nonliquidassets_ia_use nonliquidassets_ia_useterm2 nonliquidassets_ia_useterm3 nonliquidassets_ia_useterm4 liquidassets_ia_use liquidassets_ia_useterm2 liquidassets_ia_useterm3 liquidassets_ia_useterm4 homelandassets_ia_use nonhlassets_ia_use homelandassets_ia_useterm2 nonhlassets_ia_useterm2 homelandassets_ia_useterm3 nonhlassets_ia_useterm3 homelandassets_ia_useterm4 nonhlassets_ia_useterm4 laborincome_ia_use nonlaborincome_ia_use laborincome_ia_useterm2 nonlaborincome_ia_useterm2 laborincome_ia_useterm3 nonlaborincome_ia_useterm3 laborincome_ia_useterm4 nonlaborincome_ia_useterm4, i(pid_link) j(mxfls)


local varlist1 rbuc rbuc_nga inflation grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co2 grbrprovSD_lam0co2 grbrprovAvg_lam0co3 grbrprovSD_lam0co3 married hhsize hhsize_sq feelsafehome fear_assaultday fear_assaultnight hhviol times_assaulted disaster assets_ia_use income_ia_use savingnettotal_ia_use homiciderate assets_ia_useterm2 assets_ia_useterm3 assets_ia_useterm4 income_ia_useterm2 income_ia_useterm3 income_ia_useterm4 nonliquidassets_ia_use nonliquidassets_ia_useterm2 nonliquidassets_ia_useterm3 nonliquidassets_ia_useterm4 liquidassets_ia_use liquidassets_ia_useterm2 liquidassets_ia_useterm3 liquidassets_ia_useterm4 homelandassets_ia_use nonhlassets_ia_use homelandassets_ia_useterm2 nonhlassets_ia_useterm2 homelandassets_ia_useterm3 nonhlassets_ia_useterm3 homelandassets_ia_useterm4 nonhlassets_ia_useterm4 laborincome_ia_use nonlaborincome_ia_use laborincome_ia_useterm2 nonlaborincome_ia_useterm2 laborincome_ia_useterm3 nonlaborincome_ia_useterm3 laborincome_ia_useterm4 nonlaborincome_ia_useterm4 

foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'3-`var'2
}

foreach var of varlist Eddum*3  *decdum*3 {
local im=substr("`var'",1,length("`var'")-1)

gen `im'dif=.
replace `im'dif=`var'-`im'2
}

*Generate cohort by birthyear variables to cluster on
gen coh_prov_clust=yearprov

*Table H.1 - Regressions
local x rbuc_nga
local rhsvari oldmexonly
local rhsvaruse _lam0co3dif
local usei grbr
local macrouse `usei'provAvg`rhsvaruse' `usei'provSD`rhsvaruse'

cd "$reploc/results"


*Column 1
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif, vce(cluster coh_prov_clust)
outreg2 using TableH1_Mexico.tex, replace label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 2
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif assets_ia_useterm?dif , vce(cluster coh_prov_clust)
outreg2 using TableH1_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 3
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif assets_ia_usedecdum*dif , vce(cluster coh_prov_clust)
outreg2 using TableH1_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 4
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif liquidassets_ia_usedif nonliquidassets_ia_usedif, vce(cluster coh_prov_clust)
outreg2 using TableH1_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 5
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif liquidassets_ia_usedif nonliquidassets_ia_usedif liquidassets_ia_useterm?dif nonliquidassets_ia_useterm?dif , vce(cluster coh_prov_clust)
outreg2 using TableH1_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 6
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif homelandassets_ia_usedif nonhlassets_ia_usedif, vce(cluster coh_prov_clust)
outreg2 using TableH1_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 7
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif homelandassets_ia_usedif nonhlassets_ia_usedif homelandassets_ia_useterm?dif nonhlassets_ia_useterm?dif , vce(cluster coh_prov_clust)
outreg2 using TableH1_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)


*Table H.2 - Regressions
local x rbuc_nga
local rhsvari oldmexonly
local rhsvaruse _lam0co3dif
local usei grbr
local macrouse `usei'provAvg`rhsvaruse' `usei'provSD`rhsvaruse'

cd "$reploc/results"


*Column 1
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif, vce(cluster coh_prov_clust)
outreg2 using TableH2_Mexico.tex, replace label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 2
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif income_ia_useterm?dif , vce(cluster coh_prov_clust)
outreg2 using TableH2_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 3
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif income_ia_usedecdum*dif , vce(cluster coh_prov_clust)
outreg2 using TableH2_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 4
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif laborincome_ia_usedif nonlaborincome_ia_usedif, vce(cluster coh_prov_clust)
outreg2 using TableH2_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

*Column 5
reg `x'dif  `macrouse'  inflationdif marrieddif hhsizedif hhsize_sqdif Eddum*dif income_ia_usedif assets_ia_usedif savingnettotal_ia_usedif feelsafehomedif fear_assaultdaydif fear_assaultnightdif hhvioldif times_assaulteddif homicideratedif disasterdif laborincome_ia_usedif nonlaborincome_ia_usedif laborincome_ia_useterm?dif nonlaborincome_ia_useterm?dif , vce(cluster coh_prov_clust)
outreg2 using TableH2_Mexico.tex, append label  keep(`macrouse') addtext(\Delta Demographics, X, \Delta Income, X, \Delta Assets, X, \Delta Savings, X, \Delta Violence, X, \Delta Natural Disasters, X) stats(coef se)  sortvar(`macrouse') dec(2) sdec(2)

